package ruc.DolphinDB.write;

import com.xxdb.DBConnection;
import com.xxdb.data.BasicIntVector;
import com.xxdb.data.BasicLongVector;
import com.xxdb.data.BasicStringVector;
import com.xxdb.data.Entity;
import ruc.Consts.constant;
import ruc.DolphinDB.DolphinDB_Common;
import ruc.utils.IOUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class DolphinDB_DifferentRecordWrite {
    private int port;
    private String IP,tbName;
    private DBConnection conn;

    private void init() {
        try {
            this.conn = DolphinDB_Common.getDBConnection(IP, port); // 复用链接来降低时延
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public DolphinDB_DifferentRecordWrite(String IP, int port,String tbName) {
        this.IP = IP;
        this.port = port;
        this.tbName = tbName;
        init();
    }

    private long convertAndSend(List<String> list) throws IOException {
        List<Integer> cpu_usage = new LinkedList<>(), memory_usage = new LinkedList<>(), disk_usage = new LinkedList<>();
        List<String>  userInfo = new LinkedList<>(), server = new LinkedList<>(), hostname = new LinkedList<>();
        List<Long> ts = new LinkedList<>();

        for(int k = 0; k < list.size(); k++) {
            String[] v = list.get(k).split(constant.Delimiter);

            ts.add(Long.parseLong(v[0]));
            cpu_usage.add(Integer.parseInt(v[4])); memory_usage.add(Integer.parseInt(v[5])); disk_usage.add(Integer.parseInt(v[6]));
            userInfo.add(v[1]); server.add(v[2]); hostname.add(v[3]);
        }

        List<Entity> args = Arrays.asList(new BasicLongVector(ts),
                new BasicStringVector(userInfo), new BasicStringVector(server), new BasicStringVector(hostname),
                new BasicIntVector(cpu_usage), new BasicIntVector(memory_usage), new BasicIntVector(disk_usage));

        long st_time = System.nanoTime();
        conn.run(String.format("tableInsert{%s}", tbName), args); // batch insert

        return (System.nanoTime() - st_time) / constant.Ns2Ms;
    }

    private void insert(int[] recordLength, String[] val) throws IOException {
        for (int value : recordLength) {
            ArrayList<String> list = new ArrayList<>();
            long costTime = 0;
            long len = 0;
            for (String s : val) {
                len += s.getBytes().length;
                list.add(s);
                if (len > value) {
                    costTime += convertAndSend(list);
                    list = new ArrayList<>();
                    len = 0;
                }
            }

            if (list.size() > 0) {
                costTime += convertAndSend(list);
            }

            System.out.printf("[TSBenchD] [DolphinDB_DifferentRecordWrite] >>>>> Record Length %d Byte, Number of lines: %d 行， costTime: %d ms >>>>> \n", value, val.length, costTime);
            IOUtils.WriteData(constant.GetWriteFilePath("DolphinDB", "DifferentRecordWrite"),
                    new String[]{String.valueOf(System.nanoTime()), String.valueOf(value), String.valueOf(val.length), String.valueOf(costTime)});
        }
    }

    public static void main(String[] args) throws IOException {
        DolphinDB_DifferentRecordWrite db = new DolphinDB_DifferentRecordWrite(constant.IP, constant.DolphinDB_Port, constant.DifferentRecordWrite_tbName);
        db.insert(constant.DiffRecordLength, IOUtils.GetData());
    }
}
